﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //var mobiles = ChinaUnicomUtil.GetMobiles();
            //Log.Write("aa");

            //Test();

            //var code = QttHelper.GenerateDeviceCode();
            //Console.WriteLine(code);

//            var dataBytes = Convert.FromBase64String(@"W3siaWQiOjIwLCJuYW1lIjoi54i4IiwidGVsZXBob25lIjpbIjE4OTMxOTIyNTE2Il19LHsiaWQi
//OjEwMywibmFtZSI6IueZveW3pe+8iOS4gOe6v+S4gOWbvu+8iSIsInRlbGVwaG9uZSI6WyIxMzYw
//MTMzNjI0MiJdfSx7ImlkIjoxMTksIm5hbWUiOiLpmYjmjK/lrrYiLCJ0ZWxlcGhvbmUiOlsiMTM2
//NzEwMDI5MDMiXX0seyJpZCI6MjcsIm5hbWUiOiLmiL/kuJwo5bq35rC45YW0KSIsInRlbGVwaG9u
//ZSI6WyIxNTgwMTAyODU4MSJdfSx7ImlkIjoyNCwibmFtZSI6IumrmOawuOiJryIsInRlbGVwaG9u
//ZSI6WyIxNTgxMDI4MDI5MiJdfSx7ImlkIjoyMiwibmFtZSI6IuiAv+esoOiMlyIsInRlbGVwaG9u
//ZSI6WyIxNTIwMTU5MDMzOCJdfSx7ImlkIjoxNywibmFtZSI6IuiDoea0qumjniIsInRlbGVwaG9u
//ZSI6WyIxODYzMDYyNzMxNiJdfSx7ImlkIjoxLCJuYW1lIjoi5a6i5pyN5Yqp5omLIiwidGVsZXBo
//b25lIjpbIjQwMDc4ODMzMzMiXX0seyJpZCI6NCwibmFtZSI6IuWtlOWWhumRqyIsInRlbGVwaG9u
//ZSI6WyIxNTkxMDg2MDA4OSJdfSx7ImlkIjoxOCwibmFtZSI6IuadjuWFieaYpSjmnY7mgLspIiwi
//dGVsZXBob25lIjpbIjEzOTExODg0NTcwIl19LHsiaWQiOjEwLCJuYW1lIjoi5p2O5YCp5LqRIiwi
//dGVsZXBob25lIjpbIjEzNTAxMTc0MzI2Il19LHsiaWQiOjI1LCJuYW1lIjoi5qKB5qCLIiwidGVs
//ZXBob25lIjpbIjE4ODMyOTIwNzg3IiwiMTc3MzI5NjAxNzYiXX0seyJpZCI6OSwibmFtZSI6Iue9
//l+aVjyIsInRlbGVwaG9uZSI6WyIxMzkzMDExODEyNiJdfSx7ImlkIjoyMSwibmFtZSI6IuWmiCIs
//InRlbGVwaG9uZSI6WyIxMzkzMjkxMDc4NiIsIjE4OTMxOTIyNTA2Il19LHsiaWQiOjEzLCJuYW1l
//Ijoi6ams5Lqa5b69IiwidGVsZXBob25lIjpbIjEzODMxOTExOTkxIl19LHsiaWQiOjExLCJuYW1l
//Ijoi6ams5oKmIiwidGVsZXBob25lIjpbIjE4ODExMzc3ODk2Il19LHsiaWQiOjEyLCJuYW1lIjoi
//5a2f55u45a6PIiwidGVsZXBob25lIjpbIjE4NTExOTcwNzE1Il19LHsiaWQiOjI2LCJuYW1lIjoi
//5a2f56Wl5rabIiwidGVsZXBob25lIjpbIjEzMTAyNTk2ODgzIl19LHsiaWQiOjYsIm5hbWUiOiLl
//pbblpbYiLCJ0ZWxlcGhvbmUiOlsiMTU4MzM2MDE3MzUiXX0seyJpZCI6MTUsIm5hbWUiOiLluIjl
//gKnlgKkiLCJ0ZWxlcGhvbmUiOlsiMTU4MzMxOTYwMTEiXX1d
//");
//            var str = System.Text.Encoding.UTF8.GetString(dataBytes);
//            Console.WriteLine(str);

            ReadConfig();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmMain());
        }

        static void ReadConfig()
        {
            if(ConfigurationManager.AppSettings.AllKeys.Contains("DeviceCode"))
                QttHelper.SetDeviceCode(ConfigurationManager.AppSettings["DeviceCode"]);
        }

        class ThreadParam
        {
            public System.Threading.Semaphore Sem;
            public bool StopNeeded;
        }

        static void Test()
        {
            var tp = new ThreadParam();
            tp.Sem = new Semaphore(3,3);
            tp.StopNeeded = false;

            for (int i = 0; i < 10; i++)
            {
                new Thread(TestThread)
                {
                    Name = i.ToString()
                }.
                Start(tp);
            }
        }

        static void TestThread(object obj)
        {
            var tp = obj as ThreadParam;
            Console.WriteLine("{0} 等待", System.Threading.Thread.CurrentThread.Name);
            tp.Sem.WaitOne();
            if (Thread.CurrentThread.Name == "3")
            {
                tp.StopNeeded = true;
            }
            if (tp.StopNeeded)
                return;
            Console.WriteLine("{0} 运行", System.Threading.Thread.CurrentThread.Name);
            Thread.Sleep(10000);
            tp.Sem.Release();
            Console.WriteLine("{0} 释放", System.Threading.Thread.CurrentThread.Name);
        }
    }
}
